package com.hs.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.HeartMark;
import com.hs.sfzy.service.HeartMarkService;
import com.hs.sfzy.util.Result;
import com.hs.sfzy.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName HeartMarkController
 * @Description TODO
 * @Author QinKunPeng
 * @Date 2024年10月24日 11:08
 * @Version 1.0
 */
@RestController
@RequestMapping("/heartMark")
public class HeartMarkController {
    @Autowired
    HeartMarkService heartMarkService;

    /**
     * 动态sql查询心理测评
     * @author QinKunPeng
     * @date 2024/10/24 11:10
     * @param heartMark
     * @return java.lang.Object
     */
    @GetMapping
    public Object getAllHeartMark(HeartMark heartMark){
        List<HeartMark> allHeartMark = heartMarkService.getAllHeartMark(heartMark);
        return ResultUtil.success().setData(allHeartMark);
    }

    /**
     *  分页查询心理测评
     * @author QinKunPeng
     * @date 2024/10/24 11:15
     * @param pageNum
     * @param pageSize
     * @param heartMark
     * @return java.lang.Object
     */
    @GetMapping("/page")
    public Object getHeartMarkByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
            HeartMark heartMark){
        PageInfo<HeartMark> heartMarkByPage = heartMarkService.getHeartMarkByPage(pageNum, pageSize, heartMark);
        return ResultUtil.success().setData(heartMarkByPage);
    }

    /**
     *  根据id查询心理测评
     * @author QinKunPeng
     * @date 2024/10/24 11:13
     * @param id
     * @return java.lang.Object
     */
    @GetMapping("/{id}")
    public Object getHeartMarkById(@PathVariable("id") Long id){
        if (id == null){
            return ResultUtil.fail("id不能为空");
        }else{
            HeartMark heartMark = heartMarkService.getHeartMarkById(id);
            return ResultUtil.success("查询成功").setData(heartMark);
        }
    }

    /**
     * 新增一条心理测评
     * @author QinKunPeng
     * @date 2024/10/24 11:13
     * @param heartMark
     * @return java.lang.Object
     */
    @PostMapping
    public Object insertHeartMark(@RequestBody HeartMark heartMark){
        if (heartMark == null){
            return ResultUtil.fail("数据异常");
        }else{
            System.out.println(heartMark);
            int num = heartMarkService.insertHeartMark(heartMark);
            if (num > 0){
                return ResultUtil.success("添加成功");
            }else{
                return ResultUtil.fail("添加失败");
            }
        }
    }

    /**
     * 修改心理测评
     * @author QinKunPeng
     * @date 2024/10/24 11:17
     * @param heartMark
     * @return java.lang.Object
     */
    @PutMapping
    public Object updateHeartMark(@RequestBody HeartMark heartMark){
        if (heartMark == null){
            return ResultUtil.fail("数据异常");
        }else{
            int num = heartMarkService.updateHeartMark(heartMark);
            if (num > 0){
                return ResultUtil.success("修改成功");
            }else {
                return ResultUtil.fail("修改失败");
            }
        }
    }

    /**
     * 根据id删除心理测评
     * @author QinKunPeng
     * @date 2024/10/24 11:19
     * @param id
     * @return java.lang.Object
     */
    @DeleteMapping("/{id}")
    public Object deleteHeartMarkById(@PathVariable("id") Long id){
        if (id == null){
            return ResultUtil.fail("id不能为空");
        }else{
            int num = heartMarkService.deleteHeartMark(id);
            if (num > 0){
                return ResultUtil.success("删除成功");
            }else{
                return ResultUtil.fail("删除失败");
            }
        }
    }

    /**
     * 根据名称模糊查询心理测评id
     * @author QinKunPeng
     * @date 2024/10/26 21:49
     * @param name
     * @return java.lang.Object
     */
    @GetMapping("/markids")
    public Object getHeartMarkIds(String name){
        List<Integer> heartMarkIds = heartMarkService.getHeartMarkIds(name);
        return ResultUtil.success().setData(heartMarkIds);
    }

    @GetMapping("/findAnyInfo")
    public Result<List<HeartMark>> findAnyInfo(){
        return Result.success(heartMarkService.findAnyInfo());
    }




}
